/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

package au.com.lastweekend.jim.swing;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;

import javax.swing.Icon;

import au.com.lastweekend.jim.util.ImageUtils;

/**
 * mage.getHeight(null); This icon always paints an image rotated and scaled to fit inside the supplied width/height.
 * 
 * Lets make it zoomable by percentage. Fit to screen (this class!)
 * 
 * @author ggardner
 * @version $Id: AutoScaleIcon.java,v 1.2 2006/02/22 12:26:32 ggardner Exp $
 */
public class AutoScaleIcon implements Icon {

    private Image image;
    private int rotationAngle;
    private int width;
    private int height;

    /**
     * Create an AutoScaleIcon
     * 
     * @param image
     * @param angle
     *            in degrees that image will be rotated for display
     */
    public AutoScaleIcon(Image image, int angle) {

        this(image, angle, image.getWidth(null), image.getHeight(null));

    }

    public AutoScaleIcon(Image image, int angle, int width, int height) {

        this.image = image;
        this.rotationAngle = angle;
        setDimensions(width, height);

    }

    public final void setDimensions(int width, int height) {

        this.width = width;
        this.height = height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {

        Graphics2D g2 = (Graphics2D) g; // We're farked if this is not a G2D.

        g2.setBackground(c.getBackground());
        g2.clearRect(x, y, width, height);

        double iWidth = image.getWidth(null);
        double iHeight = image.getHeight(null);

        double cWidth = width;
        double cHeight = height;

        double angle = Math.toRadians(rotationAngle);

        AffineTransform at = ImageUtils.rotateAndScale(x, y, iWidth, iHeight, cWidth, cHeight, angle);

        g2.drawImage(image, at, null);
    }

    /**
     * 
     * As the icon will be auto scaled and rotated this is not very useful.
     * 
     * @return Width of supplied image
     */
    public int getIconWidth() {

        return width;
    }

    /**
     * 
     * As the icon will be auto scaled and rotated this is not very useful.
     * 
     * @return Height of supplied image
     */
    public int getIconHeight() {

        return height;
    }

}
